const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:"",
    nickName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({title:'加载中...'})
    var that = this;
    wx.getUserInfo({
      success:function(res){
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName:res.userInfo.nickName,
        })
      }
    })

    wx.login({
      success(res){
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?',
          data:{
            appid:'wx058aade237bbb63f',
            secret:'1a7e3d8d70f1022620d7a2f1786c7263',
            js_code:res.code,
            grant_type:'authorization_code'
          },
          method:"GET",
          success(Userres){
            console.log("openID:"+Userres.data.openid)
            wx.cloud.callFunction({
              name:"database_connect",
              data:{
                function_name:"findone",//方法名字
                name_collection:"teacher",//集合名称
                condition:{_openid:Userres.data.openid},//条件
              },
              success(teaSucc){
                console.log(teaSucc);
                if(teaSucc.result.data.length==0){
                  wx.cloud.callFunction({
                    name:"database_connect",
                    data:{
                      function_name:"findone",//方法名字
                      name_collection:"student",//集合名称
                      condition:{_openid:Userres.data.openid},//条件
                    },
                    success(stuSucc){
                      if(stuSucc.result.data.length==0){
                        wx.redirectTo({url: '/pages/register/register?data='+[that.data.nickName,that.data.avatarUrl]})
                      }
                      else{
                        console.log(stuSucc);
                        let Openid=stuSucc.result.data[0]._openid;
                        let Class = stuSucc.result.data[0].student_Class;
                        let Name=stuSucc.result.data[0].user_name;
                        let Id=stuSucc.result.data[0].student_id;
                        wx.redirectTo({url: '/pages/student/student?Info='+[Openid,Class,Name,Id,that.data.nickName,that.data.avatarUrl]})
                      }
                    }
                  })

                }
                else{
                  let Openid=teaSucc.result.data[0]._openid;
                  let Level = teaSucc.result.data[0].level;
                  let Name=teaSucc.result.data[0].user_name;
                  let Id=teaSucc.result.data[0].teacher_id;
                  wx.redirectTo({url: '/pages/teacher/teacher?Info='+[Openid,Level,Name,Id,that.data.nickName,that.data.avatarUrl]})
                }
              },//成功的回调
            })
            // db.collection("teacher").where({
            //   _openid:Userres.data.openid
            // }).get()
            // .then(succ=>{
            //     console.log(succ);
            //     if(succ.data.length==0){
            //       db.collection("student").where({
            //         _openid:Userres.data.openid
            //       }).get()
            //       .then(stuSucc=>{
            //         if(stuSucc.data.length==0){                  
            //           wx.redirectTo({url: '/pages/register/register'
            //         })}
            //         else {
            //           wx.redirectTo({
            //             url: '/pages/student/student?openid='+stuSucc.data[0]._openid
            //           })
            //         }
            //       })
            //     }
            //     else {
            //       wx.redirectTo({
            //         url: '/pages/teacher/teacher?openid='+succ.data[0]._openid
            //       })
            //     }
            //   })                                                                                                                         
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})